package com.netease.nim.uikit.xpopup

import android.content.Context
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.enums.PopupPosition
import com.lxj.xpopup.interfaces.OnCancelListener
import com.lxj.xpopup.interfaces.OnConfirmListener
import com.netease.nim.uikit.R
import com.netease.nim.uikit.xpopup.popuview.*
import com.netease.nimlib.sdk.team.model.TeamMember
import www.wen.com.common.lib.data.entities.*
import www.wen.com.common.lib.extension.getString
import www.wen.com.common.lib.helper.*
import www.wen.com.common.lib.view.popuview.AgreementPopupView
import www.wen.com.common.lib.view.popuview.ServerMenuPopupView

/**
 * @author WEN
 * @Description:
 * @date 2021/3/23 18:32
 */
object CreateXPopupHelper {

    /**
     * 创标准样式的警告弹窗
     */
    fun createShowStandardAlertPopupView(entity: PopupViewEntity): StandardAlertPopupView {
        val popupView = XPopup.Builder(entity.context)
                .hasBlurBg(true)
                .isDestroyOnDismiss(true)
                .autoDismiss(entity.autoDismiss)
                .asCustom(
                        StandardAlertPopupView(
                                entity.context!!,
                                entity.standardAlertPopupViewTitle!!,
                                entity.standardAlertPopupViewContent!!,
                                entity.standardAlertPopupViewCancelContent,
                                entity.standardAlertPopupViewConfirmContent,
                                finishOnCancel = entity.finishOnCancel,
                                cancelFunction = entity.cancelFunction,
                                confirmFunction = entity.confirmFunction
                        )
                )
        if (entity.isShow) {
            popupView.show()
        }
        return popupView as StandardAlertPopupView
    }

    /**
     * 创标选择区号列表的弹窗
     */
    fun createBottomAreaCodeListPopupView(entity: PopupViewEntity): BottomAreaCodeListPopupView {
        val popupView = XPopup.Builder(entity.context)
                .hasBlurBg(true)
                .isDestroyOnDismiss(true)
                .asCustom(
                        BottomAreaCodeListPopupView(
                                context = entity.context!!,
                                itac = entity.itac,
                                data = entity.areaCodeList!!,
                                resultCallBack = entity.areaCodeListResultCallBack!!
                        )
                )
        if (entity.isShow) {
            popupView.show()
        }
        return popupView as BottomAreaCodeListPopupView
    }

    /**
     * 创建用户详情的弹窗
     */
    fun createBottomUserDetailPopupView(entity: PopupViewEntity): BottomUserDetailPopupView {
        val popupView = XPopup.Builder(entity.context)
                .hasBlurBg(true)
                .isDestroyOnDismiss(true)
                .asCustom(
                        BottomUserDetailPopupView(
                                entity.context!!,
                                entity.userDetailIsMyFriend,
                                entity.userDetailOnlineStatus,
                                entity.userDetailUserInfo!!,
                                entity.userDetailResultUiCallBack!!
                        )
                )
        if (entity.isShow) {
            popupView.show()
        }
        return popupView as BottomUserDetailPopupView
    }

    /**
     * 创建首页-服务-左上角菜单弹窗
     */
    fun createServerMenuPopupView(entity: PopupViewEntity): ServerMenuPopupView {
        val popupView = XPopup.Builder(entity.context)
                .hasBlurBg(false)
                .isDestroyOnDismiss(true)
                .offsetY(1000)
                .offsetX(1000)
                .popupPosition(PopupPosition.Bottom)
                .asCustom(
                        ServerMenuPopupView(
                                entity.context!!,
                                entity.uiCallBack!!,
                        )
                )
        if (entity.isShow) {
            popupView.show()
        }
        return popupView as ServerMenuPopupView
    }

    /**
     * 创建确认信息的popupView
     */
    fun createInformPopupView(entity: PopupViewEntity): InformPopupView {
        val popupView = XPopup.Builder(entity.context)
                .hasBlurBg(false)
                .dismissOnBackPressed(false)
                .dismissOnTouchOutside(false)
                .isDestroyOnDismiss(true)
                .asCustom(
                        InformPopupView(
                                context = entity.context!!,
                                title = entity.title,
                                content = entity.content,
                                confirmContent = entity.confirmContent,
                                confirmFunction = entity.confirmFunction,
                        )
                )
        if (entity.isShow) {
            popupView.show()
        }
        return popupView as InformPopupView
    }

    /**
     * 创建底部邀请好友的PopupView
     */
    fun createBottomSharePopupView(entity: PopupViewEntity): BottomSharePopupView {
        val popupView = XPopup.Builder(entity.context)
                .hasBlurBg(false)
                .isDestroyOnDismiss(true)
                .popupPosition(PopupPosition.Bottom)
                .asCustom(
                        BottomSharePopupView(
                                entity.context!!,
                                entity.address,
                                entity.resultCallBack,
                        )
                )
        if (entity.isShow) {
            popupView.show()
        }
        return popupView as BottomSharePopupView
    }

    /**
     * 创建输入用户昵称、邮箱、个性签名的弹窗
     */
    fun createInputContextPopupView(entity: PopupViewEntity): InputContextPopupView {
        val popupView = XPopup.Builder(entity.context)
                .hasBlurBg(false)
                .isDestroyOnDismiss(true)
                .popupPosition(PopupPosition.Bottom)
                .asCustom(
                        InputContextPopupView(
                                context = entity.context!!,
                                title = entity.title,
                                hintStr = entity.hintStr!!,
                                content = entity.content,
                                cancelContent = entity.cancelContent,
                                maxLengthIn = entity.maxLength,
                                confirmContent = entity.confirmContent,
                                resultCallBack = entity.resultCallBack,
                        )
                )
        if (entity.isShow) {
            popupView.show()
        }
        return popupView as InputContextPopupView
    }


    /**
     * 创建输入选择用户性别的弹框
     */
    fun createGenderSelectionPopupView(entity: PopupViewEntity): GenderSelectionPopupView {
        val popupView = XPopup.Builder(entity.context)
                .hasBlurBg(false)
                .isDestroyOnDismiss(true)
                .popupPosition(PopupPosition.Bottom)
                .asCustom(
                        GenderSelectionPopupView(
                                context = entity.context!!,
                                mGenderSelectionType = entity.genderSelectionType,
                                resultCallBack = entity.resultGenderCallBack,
                        )
                )
        if (entity.isShow) {
            popupView.show()
        }
        return popupView as GenderSelectionPopupView
    }

    /**
     * 创建选择地区的弹框
     */
    fun createBottomAreaSelectPopupView(entity: PopupViewEntity): BottomAreaSelectPopupView {
        val popupView = XPopup.Builder(entity.context)
                .hasBlurBg(false)
                .isDestroyOnDismiss(true)
                .asCustom(
                        BottomAreaSelectPopupView(
                                context = entity.context!!,
                                province = entity.province,
                                city = entity.city,
                                options1Items = entity.options1Items!!,
                                options2Items = entity.options2Items!!,
                                options3Items = entity.options3Items!!,
                                resultCallBack = entity.resultCallBackArea,
                        )
                )
        if (entity.isShow) {
            popupView.show()
        }
        return popupView as BottomAreaSelectPopupView
    }

    /**
     * 创建日期选择器
     */
    fun createBottomDateSelectPopupView(entity: PopupViewEntity): BottomDateSelectPopupView {
        val popupView = XPopup.Builder(entity.context)
                .hasBlurBg(false)
                .isDestroyOnDismiss(true)
                .asCustom(
                        BottomDateSelectPopupView(
                                context = entity.context!!,
                                timeStr = entity.timeStr,
                                resultCallBack = entity.resultCallBackTime,
                        )
                )
        if (entity.isShow) {
            popupView.show()
        }
        return popupView as BottomDateSelectPopupView
    }

    /**
     * 创建加入服务器预览的弹框
     */
    fun createBottomJoinServerPopupView(entity: PopupViewEntity): BottomJoinServerPopupView {
        val popupView = XPopup.Builder(entity.context)
                .hasBlurBg(false)
                .isDestroyOnDismiss(true)
                .asCustom(
                        BottomJoinServerPopupView(
                                context = entity.context!!,
                                serverEntity = entity.inviteesEntity!!,
                                resultCallBack = entity.resultCallBackInvitees,
                        )
                )
        if (entity.isShow) {
            popupView.show()
        }
        return popupView as BottomJoinServerPopupView
    }

    /**
     * 创建底部分组、频道 排序 的选择弹框
     */
    fun createBottomGroupPopupView(entity: PopupViewEntity): BottomGroupPopupView {
        val popupView = XPopup.Builder(entity.context)
                .hasBlurBg(false)
                .isDestroyOnDismiss(true)
                .asCustom(
                        BottomGroupPopupView(
                                context = entity.context!!,
                                resultCallBack = entity.resultCallBack,
                        )
                )
        if (entity.isShow) {
            popupView.show()
        }
        return popupView as BottomGroupPopupView
    }

    /**
     * 创建底部分享海报的弹框
     */
    fun createBottomSharePosterPopupView(entity: PopupViewEntity): BottomSharePosterPopupView {
        val popupView = XPopup.Builder(entity.context)
                .hasBlurBg(false)
                .isDestroyOnDismiss(true)
                .asCustom(
                        BottomSharePosterPopupView(
                                context = entity.context!!,
                                content = entity.protocolContent!!,
                                resultCallBack = entity.resultProtocolEntityCallBack!!,
                        )
                )
        if (entity.isShow) {
            popupView.show()
        }
        return popupView as BottomSharePosterPopupView
    }

    /**
     * 创建协议的弹窗
     */
    fun createAgreementPopupView(entity: PopupViewEntity): AgreementPopupView {
        val popupView = XPopup.Builder(entity.context)
            .hasBlurBg(false)
            .dismissOnTouchOutside(false)
            .dismissOnBackPressed(false)
            .isDestroyOnDismiss(true)
            .asCustom(
                AgreementPopupView(
                    context = entity.context!!,
                    resultAgreementCallBack = entity.resultAgreementCallBack!!,
                    resultCallBack = entity.resultClickCallBack!!,
                )
            )
        if (entity.isShow) {
            popupView.show()
        }
        return popupView as AgreementPopupView
    }


}

class PopupViewEntity {

    /**
     * 公共参数
     */
    var context: Context? = null
    var autoDismiss: Boolean = true

    /**
     * createMachineListPopupView
     */
    var startPosition: Int = 0
    var isShow: Boolean = false
    var itemClickListener: OnItemClickListenerAgency? = null


    //createShowScanResultPopupView
    var scanResult: String? = null

    //createBottomDateSelectPopupView
    var time: Long? = null
    var key: Long? = null

    //createShowBottomFontDownLoadPopupView
    var key1: Long? = null
    var type: Int? = null

    //BottomColumnNoPopupView
    var uiCallBack: UiCallBack? = null

    /**
     * BottomQrCodePopupView 的参数
     */
    var key3: Long? = null
    var is2Dcode: Boolean = true

    /**
     * StandardAlertPopupView 的参数
     */
    var standardAlertPopupViewTitle: String? = null
    var standardAlertPopupViewContent: String? = null
    var standardAlertPopupViewCancelContent: String = "取消"
    var standardAlertPopupViewConfirmContent: String = "删除"
    var cancelFunction: CancelFunction? = null
    var finishOnCancel: Boolean = true
    var confirmFunction: ConfirmFunction? = null

    /**
     * BottomTakePhotoOptionPopupView 的参数
     */
    var bottomTakePhotoOptionPopupViewContent: CharSequence? = null
    var bottomTakePhotoOptionPopupViewCancelFunction: CancelFunction? = null
    var bottomTakePhotoOptionPopupViewFunction: BottomOptionFunction? = null

    /**
     * BottomMachineListPopupView 的参数
     */
    var machineStartIndex: Int = -1


    /**
     * AlertPopupView 的参数
     */
    var alertContent: CharSequence? = null
    var alertCancelText: CharSequence? = null
    var alertConfirmText: CharSequence? = null
    var alertConfirmListener: OnConfirmListener? = null
    var alertCancelListener: OnCancelListener? = null

    /**
     * AffirmPopupView 的参数
     */
    var affirmContent: CharSequence? = null
    var affirmCancelText: CharSequence? = null
    var affirmConfirmText: CharSequence? = null
    var affirmConfirmListener: OnConfirmListener? = null
    var affirmCancelListener: OnCancelListener? = null

    /**
     * BottomAreaCodeListPopupView 的参数
     */
    var areaCodeList: List<AreaCodeEntity>? = null
    var itac: Int = 86
    var areaCodeListResultCallBack: ResultCallBack<AreaCodeEntity>? = null

    /**
     * BottomUserDetailPopupView 的参数
     */
    var userDetailIsMyFriend: Boolean = true
    var userDetailOnlineStatus: Boolean = false
    var userDetailUserInfo: TeamMember? = null
    var userDetailResultUiCallBack: ResultUiCallBack<TeamMember>? = null

    /**
     * InformPopupView 的参数
     */
    var title: CharSequence = ""
    var content: CharSequence = ""
    var confirmContent: CharSequence = getString(R.string.confirm)

    /**
     * BottomSharePopupView 的参数
     */
    var address: String = ""
    var resultCallBack: ResultUiCallBack<String>? = null

    var resultCallBackTime: ResultUiCallBack<Long>? = null

    var resultCallBackArea: ResultUiCallBack<CustomAreaEntity>? = null

    /**
     * BottomSharePopupView 的参数
     */
    var cancelContent: CharSequence = ""
    var maxLength: Int = 8
    var hintStr: String? = null

    var resultGenderCallBack: ResultUiCallBack<GenderSelectionType>? = null
    var genderSelectionType: GenderSelectionType? = null

    var options1Items: List<CityEntity.Content>? = null
    var options2Items: MutableList<MutableList<CityEntity.Content>>? = null
    var options3Items: MutableList<MutableList<MutableList<CityEntity.Content>>>? = null
    var province: String? = null
    var city: String? = null

    var timeStr: String? = null

    var inviteesEntity: InviteesServerEntity? = null
    var resultCallBackInvitees: ResultUiCallBack<InviteesServerEntity>? = null


    var protocolContent: ProtocolEntity.Content? = null
    var resultProtocolEntityCallBack: ResultUiCallBack<ProtocolEntity.Content>? = null


    var resultAgreementCallBack: ResultUiCallBack<String>? = null

    var resultClickCallBack: ResultUiCallBack<String>? = null

    interface OnItemClickListenerAgency {

        fun onItemClickCallBack(view: View, holder: RecyclerView.ViewHolder, position: Int)

    }

}
